package reflectdemo_0406;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    private static void reflectNewInstance() {
        Class<?> c1 ;
        try {
            c1 = Class.forName("reflectdemo_0406.Student");
                                //注意要强制类型转换
            Student student = (Student) c1.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private static void reflectPrivateConstructor() {
        Class<?> c;
        try {
            c = Class.forName("reflectdemo_0406.Student");
                                                          //Declare  的才可以获得私有的
            Constructor<Student> constructor = (Constructor) c.getDeclaredConstructor(String.class, int.class);
            //因为是私有所以要设置为可以访问的，保证安全
            constructor.setAccessible(true);

            Student student = constructor.newInstance("zhangsan",18);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private static void reflectPrivateField() {
        Class<?> c;
        try {
            c = Class.forName("reflectdemo_0406.Student");
            //存在多个Field(在不同的包中),所以要注意引用对应的
            //此为reflect下的，(后面的方法同样道理)    // 成员属性的名字
            Field field = c.getDeclaredField("name");
            //同样要设置
            field.setAccessible(true);

            Student student = (Student) c.newInstance();
            //Student student = new Student();   这种也可以，但是要将对应的(异常抛出)删去

            field.set(student,"wangwu");

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private static void reflectPrivateMethod() {
        Class<?> c;
        try {
            c = Class.forName("reflectdemo_0406.Student");
            Method method = c.getDeclaredMethod("function", String.class);

            Student student = (Student) c.newInstance();
            method.setAccessible(true);


            method.invoke(student,"我是一个参数");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

}
